//
//  HomeViewController.m
//  南京大牌档
//
//  Created by student on 13-12-25.
//  Copyright (c) 2013年 无锡第三团队. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCell.h"
#import "DpViewController.h"
#import "MainViewController.h"
#import "PhoneNumberViewController.h"
#import "Reachability.h"

@interface HomeViewController (IIViewDeckControllerDelegate)

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"返回";
 
    }
    return self;
}

-(void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:[UIScreen mainScreen] .applicationFrame];
    view.backgroundColor = [UIColor yellowColor];
    self.view = view;
    
    [self initAllView];
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nagation.png"]];

    
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
	[self requsetData];
}

-(void)initAllView
{
    hometableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WXHLApplicationBounds().size.width, WXHLApplicationBounds().size.height-49) style:UITableViewStylePlain];
    hometableView.separatorColor=[UIColor colorWithRed:255/255.0 green:215/255.0 blue:0/255.0 alpha:1];
    hometableView.backgroundColor=[UIColor whiteColor];
    hometableView.dataSource=self;
    hometableView.delegate=self;
    hometableView.scrollEnabled = NO;
    
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 140)];
    _scrollView.contentSize=CGSizeMake(320*3, 160);
    _scrollView.bounces=NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled=YES;
    _scrollView.delegate = self;
    _scrollView.backgroundColor=[UIColor clearColor];
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0,120,320,20)];
    _pageControl.numberOfPages = 3;
    _pageControl.currentPage = 0;
    _pageControl.backgroundColor =[UIColor clearColor];
    
    UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, _scrollView.frame.size.height)];
    [image1 setImageWithURL:[NSURL URLWithString:@"http://www.njdapaidang.com/upload/image/123789.jpg"]];
    image1.contentMode = UIViewContentModeScaleToFill;
    image1.userInteractionEnabled=YES;
    [_scrollView addSubview:image1];
    
    UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 320, _scrollView.frame.size.height)];
    [image2 setImageWithURL:[NSURL URLWithString:@"http://www.njdapaidang.com/upload/image/1232112.jpg"]];
    image2.contentMode = UIViewContentModeScaleToFill;
    image2.userInteractionEnabled=YES;
    [_scrollView addSubview:image2];
    
    UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(640, 0, 320, _scrollView.frame.size.height)];
    [image3 setImageWithURL:[NSURL URLWithString:@"http://www.njdapaidang.com/upload/image/12455421.jpg"]];
    image3.contentMode = UIViewContentModeScaleToFill;
    image3.userInteractionEnabled=YES;
    [_scrollView addSubview:image3];
    
    hometableView.tableHeaderView=_scrollView;
    
    UITapGestureRecognizer *singleFingerOne1 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleSingleFingerEvent:)];
    UITapGestureRecognizer *singleFingerOne2 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleSingleFingerEvent:)];
    UITapGestureRecognizer *singleFingerOne3 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleSingleFingerEvent:)];
    [image1 addGestureRecognizer:singleFingerOne1];
    [image2 addGestureRecognizer:singleFingerOne2];
    [image3 addGestureRecognizer:singleFingerOne3];
 
    [self.view addSubview:hometableView];
    [hometableView addSubview:_scrollView];
    [hometableView addSubview:_pageControl];
}
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
    
//    
//    if (sender.numberOfTapsRequired!=0&&_pageControl.currentPage==0) {
//        Image1ViewController *image1View=[[Image1ViewController alloc]init];
//        [self.navigationController pushViewController:image1View animated:YES];
//        [image1View release];
//        
//    }
//    else if(sender.numberOfTapsRequired!=0&&pageControl.currentPage==1) {
//        Image2ViewController *image2View=[[Image2ViewController alloc]init];
//        [self.navigationController pushViewController:image2View animated:YES];
//        [image2View release];
//    }
//    else if(sender.numberOfTapsRequired!=0&&pageControl.currentPage==2) {
//        Image3ViewController *image3View=[[Image3ViewController alloc]init];
//        [self.navigationController pushViewController:image3View animated:YES];
//        [image3View release];
//    }
}
-(void)requsetData
{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    int month = [dateComponent month];
    int day = [dateComponent day];
    
    
    NSArray *menuArray=[NSArray arrayWithObjects:@"古法糖竽苗",@"天王烤鸭包",@"清蒸狮子头",@"金牌蜜汁藕",@"王府泡椒鸡",@"招牌盐水鸭",@"江米扣肉",@"糖醋排骨",@"苏式熏鱼",@"清卤大蹄髈",@"虾黄豆腐",@"水乡一桶鲜",@"金牌煎饺",@"牌档臭豆腐",@"酱汁蹄",@"民国美龄粥" ,nil];
    NSArray *menuImage=[[NSArray alloc]initWithObjects:@"gftym.jpg",@"twkyb.jpg",@"qzszt.jpg",@"jpmzo.jpg",@"wfpjj.jpg",@"zpysy.jpg",@"jmkr.jpg",@"tcpg.jpg",@"ssxy",@"qldtp.jpg",@"xhdf.jpg",@"sxytx.jpg",@"jpjj.jpg",@"pdcdf.jpg",@"jzt.jpg",@"mgmlz.jpg" ,nil];
    int i =(month*day)%10;
    NSDictionary *row1=@{@"title:":@"精打細算",@"smalltitle:":@"团购、打折全网收集，吃喝玩乐惠不停",@"Img:":@"youhui.jpg"};
    NSDictionary *row2=@{@"title:":@"當日推薦",@"smalltitle:":[NSString stringWithFormat:@"今日推荐的菜是:%@",menuArray[i]],@"Img:":menuImage[i]};
    NSDictionary *row3=@{@"title:":@"真誠服務",@"smalltitle:":@"真誠之味，殷情恭敬",@"Img:":@"zcfw.jpeg"};
    NSDictionary *row4=@{@"title:":@"電話預定",@"smalltitle:":@"可以直接拨打各门店电话预定",@"Img:":@"phone.jpg"};
    
    homeArray =[[NSArray alloc]initWithObjects:row1,row2,row3,row4,nil];
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage =scrollView.contentOffset.x/320;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer=@"Cell";
    HomeCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if(cell==nil)
    {
        cell=[[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    
    cell.largetext=[homeArray[indexPath.row] objectForKey:@"title:"];
    cell.smalltext=[homeArray[indexPath.row] objectForKey:@"smalltitle:"];
    cell.img=[homeArray[indexPath.row] objectForKey:@"Img:"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (WXHLApplicationBounds().size.height-140-49-44-20)/4;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0)
    {
        Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        if ([r currentReachabilityStatus]==NotReachable) {
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"警告！" message:@"请检查手机网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];

        }
        else{
            
            DpViewController*dpVC=[[DpViewController alloc]init];
            [self.navigationController pushViewController:dpVC animated:YES];
        }
        
    }
    else if (indexPath.row==2)
    {
        
    }
    else if (indexPath.row==3) {
        PhoneNumberViewController *pnVC=[[PhoneNumberViewController alloc]init];
        [self.navigationController pushViewController:pnVC animated:YES];
    }

}



@end
